﻿@{
    ViewBag.Title = "新闻列表";
}
@section SideNav{@Html.Partial("SideNavPartialView")}
<ol class="breadcrumb">
    <li><span class="glyphicon glyphicon-home"></span>  @Html.ActionLink("首页", "Index", "Home")</li>
    <li class="active">@Html.ActionLink("新闻动态", "Index", "News")</li>
</ol>

<div class="form-inline">
    <input id="search" type="text" name="QueryString" class="form-control" placeholder="输入检索词" />
    <input type="button" id="btnSearch" value="检索" class="btn btn-default" />
</div>
<hr />
<div id="toolbar" class="btn-group" role="group">
    <button id="btn_add" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span>@Html.ActionLink("添加新闻动态", "Add")</button>
    <button id="btn_Bussiness" class="btn btn-default" value="1"><span class="glyphicon glyphicon-search"></span> 只看行业动态</button>
    <button id="btn_News" class="btn btn-default" value="2"><span class="glyphicon glyphicon-search"></span> 只看新闻动态</button>
    <button id="btn_All" class="btn btn-default" value=""><span class="glyphicon glyphicon-search"></span> 全部</button>
</div>
<table id="newsgrid"></table>
<script type="text/javascript">
    let type = null
    $('#btn_Bussiness').click(function () {
        type = this.value;
        var $table = $('#newsgrid');
        $table.bootstrapTable("refresh");
    });
    $('#btn_News').click(function () {
        type = this.value;
        var $table = $('#newsgrid');
        $table.bootstrapTable("refresh");
    });
    $('#btn_All').click(function () {
        type = null;
        var $table = $('#newsgrid');
        $table.bootstrapTable("refresh");
    });

    $('#btnSearch').click(function () {
        var $table = $('#newsgrid');
        $table.bootstrapTable("refresh");
        //initTable($table);
    });
    $(document).ready(function () {
        var $table = $('#newsgrid');
        $table.bootstrapTable({
            toolbar: "toolbar",
            showRefresh: true,
            showColumns: true,
            pageNumber: 1,
            pageSize: 10,
            pagination: true,
            sidePagination: "server",
            pageList: "[5, 10, 20, 50, 100]",
            method: "post",
            contentType: "application/x-www-form-urlencoded",
            url: "@Url.Action("PageList")",
            queryParams: queryParams,
            queryParamsType: "limit", //参数格式,发送标准的RESTFul类型的参数请求
            columns: [
                    { title: "state", checkbox: true },
                    { title: "ID", field: "NewsID" },
                    { title: "标题", field: "Title" },
                    { title: "作者", field: "Author" },
                    { title: "类型", field: "ContentType", formatter: function (value) { if (value == 1) value = "行业动态"; else value = "新闻动态"; return value; } },
                    { title: "创建时间", field: "Created_at", formatter: function (value) { return moment(value).format("YYYY-MM-DD HH:mm:ss") } },
                    { title: "更新时间", field: "Updated_at", formatter: function (value) { return moment(value).format("YYYY-MM-DD HH:mm:ss") }, visible: false },
                    {
                        title: "操作", field: "NewsID", formatter: function (value) {
                            return "<a class='btn btn-sm btn-danger' data-method='Delete' data-value='" + value + "'>删除</a>&nbsp;<a href='/Control/News/Modify/" + value + "' class='btn btn-sm btn-primary'>编辑</a>&nbsp;<a href='/Control/News/Detail/" + value + "' class='btn btn-sm btn-primary'>详情</a>"
                        }
                    }
            ],
            onAll: function (name, args) {
                if ((name == "load-success.bs.table") || (name == "column-switch.bs.table")) {
                    //删除按钮
                    $("a[data-method='Delete']").click(function () {
                        var id = $(this).attr("data-value");
                        BootstrapDialog.confirm("确定要删除该项新闻", function (result) {
                            if (result) {
                                $.post("@Url.Action("Delete")", { newsid: id }, function (data) {
                                    if (data.Code == 1) {
                                        BootstrapDialog.show({
                                            message: "删除新闻成功",
                                            buttons: [{
                                                icon: "glyphicon glyphicon-ok",
                                                label: "确定",
                                                action: function (dialogItself) {
                                                    $table.bootstrapTable("refresh");
                                                    dialogItself.close();
                                                }
                                            }]
                                        });
                                    }
                                    else BootstrapDialog.alert(data.Message);
                                }, "json");
                            }
                        });
                    });
                    //删除结束
                }
            }
        });
        $("#btn_add").click(function () {
            location.href = "@Url.Action("Add")"
        })

    })
</script>
<script type="text/javascript">
    function queryParams(params) {
        var contentType = null;
        if (type)
            contentType = type;
        var temp = {
            pageNumber: params.offset,
            pageSize: params.limit,
            keywords: document.getElementById("search").value,
            contentType: contentType
        };
        return temp;
    }
</script>